package com.captainbank.tcl.tclapplication.ui.activity.function;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.captainbank.tcl.tclapplication.R;
import com.captainbank.tcl.tclapplication.ui.activity.basic.BaseActivity;
import com.jakewharton.rxbinding2.view.RxView;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;

/**
 * Created by TCL on 2019/6/21.
 * Describe:RxPermissions与BaseActivity.onRequestPermissionsResult冲突
 * 要命的是RxPermissions需要在onCreate方法中执行(可在onCreate中绑定点击监听)
 * 但是可以通过RxBinding进行条件触发，如点击按钮触发，触发内容同样需要在onCreate中先进行声明
 */
public class RxPermissionsActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.btn1)
    Button btn1;
    @BindView(R.id.btn2)
    Button btn2;
    @BindView(R.id.btn3)
    Button btn3;
    @BindView(R.id.btn4)
    Button btn4;
    @BindView(R.id.btn5)
    Button btn5;
    @BindView(R.id.btn6)
    Button btn6;
    @BindView(R.id.btn7)
    Button btn7;
    @BindView(R.id.tv_show)
    TextView tvShow;

    private RxPermissions rxPermissions;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_rxpermissions;
    }

    @Override
    protected void initView() {
        toolbar.setTitle(getString(R.string.title_permission));
        rxPermissions = new RxPermissions(this);
    }

    @SuppressLint("CheckResult")
    @Override
    protected void setListener() {
        RxView.clicks(btn1)
                .compose(new RxPermissions(this).ensure(Manifest.permission.READ_EXTERNAL_STORAGE))
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean granted) throws Exception {
                        if(granted){
                            tvShow.setText("获取读权限");
                        }else{
                            tvShow.setText("拒绝读权限");
                        }
                    }
                });
//
//        btn1.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                rxPermissions
//                        .request(Manifest.permission.READ_EXTERNAL_STORAGE)
//                        .subscribe(new Consumer<Boolean>() {
//                            @Override
//                            public void accept(Boolean granted) throws Exception {
//                                if (granted) {
//                                    tvShow.setText("获取读权限");
//                                } else {
//                                    tvShow.setText("拒绝读权限");
//                                }
//                            }
//                        });
//            }
//        });
    }


    @OnClick({R.id.btn2, R.id.btn3, R.id.btn4, R.id.btn5, R.id.btn6,R.id.btn7})
    public void onViewClicked(View view) {
        switch (view.getId()) {
//            case R.id.btn1:
//                rxPermissions
//                        .request(Manifest.permission.READ_EXTERNAL_STORAGE)
//                        .subscribe(new Consumer<Boolean>() {
//                            @Override
//                            public void accept(Boolean granted) throws Exception {
//                                if(granted){
//                                    tvShow.setText("获取读权限");
//                                }else{
//                                    tvShow.setText("拒绝读权限");
//                                }
//                            }
//                        });
//                break;
            case R.id.btn2:
                rxPermissions
                        .request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        .subscribe(new Consumer<Boolean>() {
                            @Override
                            public void accept(Boolean granted) throws Exception {
                                if(granted){
                                    tvShow.setText("获取写权限");
                                }else{
                                    tvShow.setText("拒绝写权限");
                                }
                            }
                        });
                break;
            case R.id.btn3:
                rxPermissions
                        .request(Manifest.permission.CALL_PHONE)
                        .subscribe(new Consumer<Boolean>() {
                            @Override
                            public void accept(Boolean granted) throws Exception {
                                if(granted){
                                    tvShow.setText("获取电话权限");
                                }else{
                                    tvShow.setText("拒绝电话权限");
                                }
                            }
                        });
                break;
            case R.id.btn4:
                rxPermissions
                        .request(Manifest.permission.CAMERA)
                        .subscribe(new Consumer<Boolean>() {
                            @Override
                            public void accept(Boolean granted) throws Exception {
                                if(granted){
                                    tvShow.setText("获取摄像头权限");
                                }else{
                                    tvShow.setText("拒绝摄像头权限");
                                }
                            }
                        });
                break;
            case R.id.btn5:
                rxPermissions
                        .request(Manifest.permission.READ_EXTERNAL_STORAGE
                        ,Manifest.permission.WRITE_EXTERNAL_STORAGE
                        ,Manifest.permission.CALL_PHONE
                        ,Manifest.permission.CAMERA)
                        .subscribe(new Consumer<Boolean>() {
                            @Override
                            public void accept(Boolean granted) throws Exception {
                                if(granted){
                                    tvShow.setText("获取全部权限");
                                }else{
                                    tvShow.setText("未获取全部权限");
                                }
                            }
                        });
                break;

            case R.id.btn6:
                rxPermissions
//                        .requestEach
                        .requestEachCombined
                                (Manifest.permission.READ_EXTERNAL_STORAGE
                                ,Manifest.permission.WRITE_EXTERNAL_STORAGE
                                ,Manifest.permission.CALL_PHONE
                                ,Manifest.permission.CAMERA)
                        .subscribe(new Consumer<Permission>() {

                            @Override
                            public void accept(Permission permission) throws Exception {
                                if (permission.granted) {
                                    tvShow.setText("获取全部权限");
                                } else if (permission.shouldShowRequestPermissionRationale) {
                                    tvShow.setText("未获取全部权限");
                                } else {
                                    tvShow.setText("未获取全部权限且不再询问");
                                }
                            }
                        });
                break;

            case R.id.btn7:
                Intent intent = new Intent();
                intent.setAction(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.setData(Uri.parse("package:" + getPackageName()));
                startActivity(intent);
                break;
        }
    }
}
